﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Util.Ini;

namespace svnbox {
   class Resource {
        public IniFile ini;
        public string Language = "";
        public bool UseBase64 = false;

        private void debug(string s) { System.Diagnostics.Debug.WriteLine("{Resource} " + s); }

        public Resource() {
            string Lang = App.Profile.Get("global", "language");
            ChangeLanguage(Lang);
        }

        public string Lang(string section, string key, string def) {
            if (UseBase64) {
                return GetValue_Base64(section, key, def);
            } else {
                return GetValue(section, key, def);
            }                    
        }

        public Dictionary<string, string> GetAvailableLanguages() {
            Dictionary<string, string> outp = new Dictionary<string, string>();
            string[] AvailableLanguages = System.IO.Directory.GetFiles(App.LangPath, "*.ini");
            foreach (string Lang in AvailableLanguages) {
                IniFile s_ini = new IniFile(Lang);
                string DisplayName = s_ini.GetValue("language", "DisplayName", System.IO.Path.GetFileNameWithoutExtension(Lang));
                outp.Add(System.IO.Path.GetFileNameWithoutExtension(Lang), DisplayName);
            }
            return outp;
        }

        internal void Translate(IForm frm) {
            if (frm.Language != Language) {
                frm.Language = Language;
                foreach (object o in frm.FormControls) {
                    ApplyResources(frm.Name, o);
                }
            }
        }

        internal void ChangeLanguage(string lang) {
            if (lang == Language)
                return; // Nothing to changed
            Language = lang;
            App.Profile.Set("global", "language", lang);
            ini = new IniFile(App.LangPath + lang + ".ini");
            UseBase64 = GetValue("language", "UseBase64", "0") == "1" ? true : false;

            foreach (IForm ifrm in IForm.Instances) {
                foreach (object o in ifrm.FormControls) {
                    ApplyResources(ifrm.Name, o);
                }
            }
        }

        private string GetValue(string section, string key, string def) {
            if (ini == null) return "";               
            if (ini.GetValue(section, key, "*****") == "*****") {
                ini.SetValue(section, key, def);
                return def;
            }
            return ini.GetValue(section, key, def);
        }

        private string GetValue_Base64(string section, string key, string def) {
            if (ini.GetValue(section, key, "*****") == "*****") {
                ini.SetValue(section, key, Util.Encoding.EncodeTo64(def));
                return def;
            }
            try {
                return Util.Encoding.DecodeFrom64(ini.GetValue(section, key, def));
            } catch {
                return ini.GetValue(section, key, def);
            }
        }

        internal void ApplyResources(string section, object o) {
            if (o.GetType() == typeof(MenuItem)) {
                MenuItem m = (MenuItem)o;
                if (m.Name == null) {
                    MessageBox.Show("MenuItem dosen't have .Name " + m.ToString());
                    return;
                }
                m.Text = Lang(section, m.Name, m.Text);
            } else if (o.GetType() == typeof(Label)) {
                Label l = (Label)o;
                l.Text = Lang(section, l.Name, l.Text);
            } else if (o.GetType() == typeof(Button)) {
                Button l = (Button)o;
                l.Text = Lang(section, l.Name, l.Text);
            } else if (o.GetType() == typeof(TabPage)) {
                TabPage l = (TabPage)o;
                l.Text = Lang(section, l.Name, l.Text);
            } else if (o.GetType() == typeof(GroupBox)) {
                GroupBox l = (GroupBox)o;
                l.Text = Lang(section, l.Name, l.Text);
            } else if (o.GetType() == typeof(CheckBox)) {
                CheckBox l = (CheckBox)o;
                l.Text = Lang(section, l.Name, l.Text);
            } else if (o.GetType() == typeof(svnbox.frmProfile)) {
                IForm l = (IForm)o;
                l.Text = Lang(section, l.Name, l.Text);
            }else{
                debug("Applyng resource to Other : "+ o.GetType() +  "  " + o.ToString());
            }
        }
        
    }
}
